package fr.umlv.m2.recipes.ideas;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import fr.umlv.m2.recipes.ideas.bean.IngredientBean;
import fr.umlv.m2.recipes.ideas.bean.RecipeBean;
import fr.umlv.m2.recipes.ideas.sqlite.db.objects.RecipesDB;

/**
 * Create the Search Activity view in the tab widget
 * @author Christophe Chillan
 *
 */
public class SearchWidget extends Activity {

	private Button submit;
	private AutoCompleteTextView ingredientInput;
	private ListView ingredientList;
	private EditText keywordsInput;
	private EditText nameInput;
	private Button addIngredientButton;
	private Button removeIngredientButton;
	private final List<String> ingredientsNames = new ArrayList<String>();

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.searchpanel);

		submit 					= (Button) findViewById(R.id.searchButton);
		addIngredientButton 	= (Button) findViewById(R.id.addIngredient);
		removeIngredientButton 	= (Button) findViewById(R.id.removeIngredient);
		ingredientInput 		= (AutoCompleteTextView) findViewById(R.id.ingredients);
		ingredientList 			= (ListView)findViewById(R.id.ingredientsList);	  
		
		keywordsInput 			= (EditText)findViewById(R.id.keywordsInput);
		nameInput 			    = (EditText)findViewById(R.id.nameInput);

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, getAllIngredients());
		ingredientInput.setAdapter(adapter);

		registerListeners();
	}

	private void registerListeners(){
		submit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String  [] keywords = new String[0];
				if(keywordsInput.getText().toString().trim().length()>0){
					keywords = keywordsInput.getText().toString().split(" ");
				}
				
				String name = nameInput.getText().toString();	
				
				List<RecipeBean> recipes = RecipesDB.getRecipeDao().getRecipeBySearchEngine(RecipesDB.getDatabase(), name, keywords, ingredientsNames);
				
				Bundle bundle = new Bundle();
				bundle.putSerializable("recipes", new ArrayList<RecipeBean>(recipes));

				Intent intent = new Intent(SearchWidget.this.getApplicationContext(), RecipesWidget.class);
				intent.putExtras(bundle);
				startActivity(intent);
			}
		});

		addIngredientButton.setOnClickListener( new OnClickListener() {
			@Override
			public void onClick(View v) {
				ingredientsNames.add(ingredientInput.getText().toString());
				ingredientList.setAdapter(new ArrayAdapter<String>(SearchWidget.this, android.R.layout.simple_list_item_1, ingredientsNames));
			}
		});

		removeIngredientButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(ingredientsNames.size()>0){
					ingredientsNames.remove(ingredientsNames.size()-1);
					ingredientList.setAdapter(new ArrayAdapter<String>(SearchWidget.this, android.R.layout.simple_list_item_1, ingredientsNames));
				}
			}
		});
	}

	private String [] getAllIngredients(){
		final List<IngredientBean> ingredients = RecipesDB.getIngredientDao().getAllIngredients(RecipesDB.getDatabase());
		String [] strs = new String [ingredients.size()];

		return strs;
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.mainmenu, menu);
		return true;
	}

	/**
	 * Use to Define the Option Menu
	 */
	public boolean onOptionsItemSelected(MenuItem item) {
		// On regarde quel item a été cliqué grâce à son id et on déclenche une
		// action
		switch (item.getItemId()) {
			case R.id.home:
				startActivity(new Intent(this, MenuTabWidget.class));
				return true;
			case R.id.about:
				startActivity(new Intent(this, AboutView.class));
				return true;
			default:
		        return super.onOptionsItemSelected(item);
			}
		
	}
}
